//
//  SecondController.m
//  RRIsSmallPig
//
//  Created by K。 on 15/7/24.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "SecondController.h"

@interface SecondController ()
@property (strong , nonatomic) NSString *url;
@end

@implementation SecondController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(170, 120);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    self.secCollectionView = [[UICollectionView alloc]initWithFrame: [[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.secCollectionView.backgroundColor = [UIColor colorWithRed:0.3 green:0.4 blue:0.6 alpha:1.0];
    [self.view addSubview:self.secCollectionView];
    self.secCollectionView.delegate = self;
    self.secCollectionView.dataSource = self;
    
    [self.secCollectionView registerClass:[SecCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"加载中";
    self.HUD.backgroundColor =[UIColor colorWithRed:0.3 green:0.4 blue:0.5 alpha:1.0];
    [self.HUD show:YES];
    
    self.page = 1;
    self.secCollectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page ++;
        if (self.page == 11) {
            [self.secCollectionView.footer noticeNoMoreData];
        }else{
        
            [self GetData];
            [self.secCollectionView.footer endRefreshing];
        }
    }];
    
    [self GetData];
    
}
-(void)GetData
{
    if (self.isSelected) {
        self.url = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/home/tblAssort!getVegetable.do?id=%ld&page=%ld&pageRecord=10&is_traditional=0&phonetype=1",self.typeId, self.page];
    }else{
        self.url = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueVegetableList.do?is_traditional=0&page=%ld&pageRecord=10&phonetype=0&user_id=0&typeId=%ld",self.page, self.typeId];
    
    }
   [AFN_block NSString:self.url Blokc:^(id request) {
       
       NSMutableArray *array = [request objectForKey:@"data"];
       self.secArray = [NSMutableArray array];
       for (NSMutableDictionary *dic in array) {
           SecModle *secModle = [[SecModle alloc]init];
           [secModle setValuesForKeysWithDictionary:dic];
           [self.secArray addObject:secModle];
       }
       [self.secCollectionView reloadData];
       [self.HUD hide:YES];
   }];

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return  1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return  self.secArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SecCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.secModel =[self.secArray objectAtIndex:indexPath.row];
    
    return  cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
